/*
 * Copyright (c) 2014, 2015, XIANDIAN and/or its affiliates. All rights reserved.
 * XIANDIAN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
 *
 */
package com.xiandian.cloud.storage.fragment;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import com.woorea.openstack.swift.Swift;
import com.woorea.openstack.swift.api.ContainerResource;
import com.woorea.openstack.swift.model.Container;
import com.woorea.openstack.swift.model.Object;
import com.woorea.openstack.swift.model.ObjectDownload;
import com.woorea.openstack.swift.model.ObjectForUpload;
import com.woorea.openstack.swift.model.Objects;
import com.xiandian.cloud.storage.CramerProActivity;
import com.xiandian.cloud.storage.R;
import com.xiandian.cloud.storage.base.OpenstackApplicationState;
import com.xiandian.cloud.storage.base.TaskResult;
import com.xiandian.cloud.storage.object.PseudoFileSystem;
import com.xiandian.cloud.storage.sdk.service.OpenStackClientService;
import com.xiandian.cloud.storage.signup.LoginActivity;
import com.xiandian.cloud.storage.utils.DisplayUtils;
import com.xiandian.cloud.storage.utils.GraphicsUtil;
import com.xiandian.cloud.storage.utils.PromptDialogUtil;
import android.annotation.SuppressLint;
import android.app.ActionBar;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.media.ThumbnailUtils;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Environment;
import android.preference.PreferenceManager;
import android.provider.MediaStore;
import android.provider.MediaStore.Images;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v4.widget.SwipeRefreshLayout.OnRefreshListener;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.webkit.MimeTypeMap;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.SimpleAdapter.ViewBinder;
import android.widget.Toast;

/**
 * 全部文件
 * @author 云计算应用与开发项目组
 * @since  V1.0
 */
public class ObjectListFragment extends Fragment {
	
	private static ObjectListFragment instance=null;
	
	private static final int ACTION_SELECT_CONTENT_FROM_APPS = 1;
	private static final int ACTION_SELECT_CONTENT_FROM_CAMERA=2;
	
	ListView list_root;
	SwipeRefreshLayout swipe_refresh_files;
	List<PseudoFileSystem> listPseudoFileSystem;
	List<Object> listObjects;
	
	String mCurrentPhotoPath;
	Uri mImageUri = null;
	
	AlertDialog directoryNamePromptDialog;
	AlertDialog renameDialog;
	LinearLayout layout_operation_bar;
	
	String copyFileName;
	String copyToFileName;
	String copyFileType;
	boolean isCopying=false;
	
	String moveFileName;
	String moveToFileName;
	String moveFileType;
	boolean isMoving=false;
	
	public ObjectListFragment(){
		
	}
	
	public static ObjectListFragment getInstance(){
		if(instance==null)
			instance=new ObjectListFragment();
		return instance;
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View rootView = inflater.inflate(R.layout.fragment_object_list,
				container, false);
		
		setHasOptionsMenu(true);
		
		layout_operation_bar=(LinearLayout) rootView.findViewById(R.id.layout_operation_bar);
		
		list_root=(ListView) rootView.findViewById(R.id.list_root);
		list_root.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				
			}
		});
		
		list_root.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> parent, View view,
					int position, long id) {

				return true;
			}
		});
		
		swipe_refresh_files=(SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_files);
		swipe_refresh_files.setColorSchemeColors(0,0,0,0);
		swipe_refresh_files.setOnRefreshListener(new OnRefreshListener() {

			@Override
			public void onRefresh() {
				swipe_refresh_files.setRefreshing(false);
				getActivity().setProgressBarIndeterminateVisibility(true);

			}
		});
		
		rootView.findViewById(R.id.btnConfirm).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				
			}
		});
		
		rootView.findViewById(R.id.btnCancel).setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				layout_operation_bar.setVisibility(View.GONE);
			}
		});
		
		getActivity().setProgressBarIndeterminateVisibility(true);
		
		return rootView;
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub
		inflater.inflate(R.menu.object_list, menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		int id = item.getItemId();
		switch (id) {
		case R.id.action_upload:
			 Intent intent = new Intent(getActivity(), CramerProActivity.class);  
             startActivity(intent);  
			break;
		case R.id.action_addDirectory:
			
			break;
		case R.id.action_camera:
			
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	@Override
	public void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		
	}

	/**
	 * 
	 */
	public void onBackPressed(){

	}
	
	/**
	 * 流操作
	 * @param stream
	 * @param is
	 */
	private static void write(OutputStream stream, InputStream is) {
		try {
			int bufferSize = 1024;
			byte[] buffer = new byte[bufferSize];
			int len = 0;
			while ((len = is.read(buffer)) != -1) {
				stream.write(buffer, 0, len);
			}
			stream.close();
		} catch (IOException e) {
			throw new RuntimeException(e.getMessage(), e);
		}

	}
	
	/**
	 * Clean name.
	 * 
	 * @param objectName
	 *            the object name
	 * @return the string
	 */
	private String cleanName(String objectName) {
		String[] parts = objectName.split("/");
		return parts[parts.length - 1];
	}

	private OpenstackApplicationState getApplicationState(){
		return OpenstackApplicationState.getInstance();
	}

	private OpenStackClientService getService(){
		return OpenStackClientService.getInstance();
	}

	/**
	 * 创建临时文件
	 * @return
	 * @throws IOException
	 */
	public File createTempFiles() throws IOException {
		String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss")
				.format(new Date());
		String imageFileName = "JPEG_" + timeStamp + "_";
		File storageDir = Environment
				.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
		File image = File.createTempFile(imageFileName, /* prefix */
				".jpg", /* suffix */
				storageDir /* directory */
		);

		// Save a file: path for use with ACTION_VIEW intents
		mCurrentPhotoPath = "file:" + image.getAbsolutePath();
		return image;
	}

}
